﻿#include "dlg1553.h"
#include "ui_dlg1553.h"
#include "globalstatic.h"
#include "dlgfault.h"

#include <QDebug>
#include <QIntValidator>
#include <QDateTime>
#include <QMessageBox>

Dlg1553::Dlg1553(QSettings* settings, const int level, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dlg1553),
    m_settings(settings),
    m_level(level)

{
    ui->setupUi(this);

    // 初始化
    initUI();
    init();
}

Dlg1553::~Dlg1553()
{
    delete ui;
}

void Dlg1553::initUI()
{
    setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::MSWindowsFixedSizeDialogHint);

    // 总线选择
    m_bgBus = new QButtonGroup(this);
    m_bgBus->addButton(ui->rdoBUSA, 0);
    m_bgBus->addButton(ui->rdoBUSB, 1);

    // 限制输入正整数的取值范围
    ui->leSyncInterval->setValidator(new QIntValidator(1, 999999, ui->leSyncInterval));

    connect(ui->btnLogicVersionCheck,  &QPushButton::clicked, this, &Dlg1553::onLogicVersionCheck);
    connect(ui->btnBus,  &QPushButton::clicked, this, &Dlg1553::onBus);
    connect(ui->btnSelfCheck,  &QPushButton::clicked, this, &Dlg1553::onSelfCheck);
    connect(ui->btnSync,  &QPushButton::clicked, this, &Dlg1553::onSync);
    connect(ui->btnReset,  &QPushButton::clicked, this, &Dlg1553::onReset);
    connect(ui->btnClose, &QPushButton::clicked, this, [=](){this->accept();});
}

void Dlg1553::init()
{
    m_settings->beginGroup("1553");
    int bus = m_settings->value("bus").toInt();
    int syncInterval = m_settings->value("syncInterval").toInt();
    m_settings->endGroup();

    // 总线选择
    switch (bus) {
    case 0:
        ui->rdoBUSA->setChecked(true);
        break;
    case 1:
        ui->rdoBUSB->setChecked(true);
        break;
    default:
        break;
    }

    // 同步时间
    ui->leSyncInterval->setText(QString::number(syncInterval));
}

void Dlg1553::onLogicVersionCheck()
{
    QMessageBox::information(this, QStringLiteral("提示"), QStringLiteral("逻辑版本正确!"));
}

void Dlg1553::onBus()
{
    if (ui->rdoBUSA->isChecked()) {
        QMessageBox::information(this, QStringLiteral("提示"), QStringLiteral("总线选择BUSA!"));
    }
    if (ui->rdoBUSB->isChecked()) {
        QMessageBox::information(this, QStringLiteral("提示"), QStringLiteral("总线选择BUSB!"));
    }

    m_settings->beginGroup("1553");
    m_settings->setValue("bus", m_bgBus->checkedId());
    m_settings->endGroup();
}

void Dlg1553::onSelfCheck()
{
    if (m_level == 1) {
        if (GlobalStatic::faultCount() > 0 && GlobalStatic::containFaultCode("C15101010011")) {
            emit sigFaultOccurred("C15101010011");
        } else {
            QMessageBox::information(this, QStringLiteral("提示"), QStringLiteral("自检正常!CRC检验正确!"));
        }
    }

    if (m_level == 2) {
        if (GlobalStatic::faultCount() > 0 && GlobalStatic::containFaultCode("C15102010011")) {
            emit sigFaultOccurred("C15102010011");
        } else {
            QMessageBox::information(this, QStringLiteral("提示"), QStringLiteral("自检正常!CRC检验正确!"));
        }
    }
}

void Dlg1553::onSync()
{
    // 同步时间
    int syncInterval = ui->leSyncInterval->text().trimmed().toInt();
    QTime expiredTime = QTime::currentTime().addMSecs(syncInterval);
    while (QTime::currentTime() < expiredTime)
        QApplication::processEvents(QEventLoop::AllEvents, 100);

    m_settings->beginGroup("1553");
    m_settings->setValue("syncInterval", syncInterval);
    m_settings->endGroup();

    QMessageBox::information(this, QStringLiteral("提示"), QStringLiteral("同步正常!"));
}

void Dlg1553::onReset()
{
    QMessageBox::information(this, QStringLiteral("提示"), QStringLiteral("复位正常!"));
}

